package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLog;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLogConstant;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.converter.LevelConverter;
import com.cskaoyan.wordmemorize.dao.entity.LevelPrivilegeDO;
import com.cskaoyan.wordmemorize.dao.mapper.LevelPrivilegeMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelPrivilegeOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.LevelPrivilegeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelPrivilegeCommand;
import com.cskaoyan.wordmemorize.request.LevelPrivilegePageRequest;
import com.cskaoyan.wordmemorize.service.LevelPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LevelPrivilegeServiceImpl implements LevelPrivilegeService {
    @Autowired
    LevelConverter levelConverter;
    @Autowired
    LevelPrivilegeMapper levelPrivilegeMapper;

    @Override
    public PageDTO<LevelPrivilegeDTO> getLevelPrivilegeList(LevelPrivilegePageRequest pageRequest) {
        if (pageRequest.getPageNum()== null) {
            //查询所有等级权限列表
            List<LevelPrivilegeDO> levelPrivilegeDOs = levelPrivilegeMapper.selectList(null);
            return levelConverter.levelPrivilegeDOToPageDTO(levelPrivilegeDOs, levelPrivilegeDOs.size());
        }
        //需要分页
        Page<LevelPrivilegeDO> page = new Page<>(pageRequest.getPageNum(), pageRequest.getCount());
        LambdaQueryWrapper<LevelPrivilegeDO> queryWrapper = new LambdaQueryWrapper<>();
        if (pageRequest.getKeyword() != null) {
            queryWrapper.like(LevelPrivilegeDO::getPrivilegeCode, pageRequest.getKeyword())
                    .or()
                    .like(LevelPrivilegeDO::getPrivilegeName, pageRequest.getKeyword())
            ;
        }
        if(pageRequest.getLevelId() != null)
        {
            queryWrapper.eq(LevelPrivilegeDO::getLevelId, pageRequest.getLevelId());
        }
        Page<LevelPrivilegeDO> levelPrivilegeDOPage = levelPrivilegeMapper.selectPage(page, queryWrapper);
        return levelConverter.levelPrivilegeDOToPageDTO(levelPrivilegeDOPage.getRecords(), (int) levelPrivilegeDOPage.getTotal());
    }

    @Override
    @OperateLog(dateType = "权限", operateType = OperateLogConstant.OPERATE_ADD)
    public void addLevelPrivilege(LevelPrivilegeCommand command) {
        LambdaQueryWrapper<LevelPrivilegeDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LevelPrivilegeDO::getPrivilegeName, command.getPrivilegeName());
        LevelPrivilegeDO levelPrivilegeDO = levelPrivilegeMapper.selectOne(queryWrapper);
        if (levelPrivilegeDO != null) {
            throw new BusinessException(ResultCodeEnum.LevelPrivilege_EXISTED);
        }
        LevelPrivilegeDO levelPrivilege = levelConverter.levelPrivilegeCommandToLevelPrivilegeDO(command);
        levelPrivilegeMapper.insert(levelPrivilege);

    }

    @Override
    @OperateLog(dateType = "权限", operateType = OperateLogConstant.OPERATE_UPDATE)
    public void updateLevelPrivilege(LevelPrivilegeCommand command) {
        LevelPrivilegeDO levelPrivilegeDO = levelConverter.levelPrivilegeCommandToLevelPrivilegeDO(command);
        levelPrivilegeMapper.updateById(levelPrivilegeDO);

    }

    @Override
    @OperateLog(dateType = "权限", operateType = OperateLogConstant.OPERATE_SINGLE_DELETE)
    public DeleteLevelPrivilegeOperateDTO deleteLevelPrivilege(Long id) {
        LevelPrivilegeDO levelPrivilegeDO = levelPrivilegeMapper.selectById(id);
        levelPrivilegeMapper.deleteById(id);
        DeleteLevelPrivilegeOperateDTO deleteLevelPrivilegeOperateDTO = levelConverter.levelPrivilegeDOToDeleteLevelPrivilegeOperateDTO(levelPrivilegeDO);
        return deleteLevelPrivilegeOperateDTO;
    }

    @Override
    @OperateLog(dateType = "权限", operateType = OperateLogConstant.OPERATE_BATCH_DELETE)
    public List<DeleteLevelPrivilegeOperateDTO> batchDeleteLevelPrivilege(BatchDeleteCommand command) {
        List<LevelPrivilegeDO> levelPrivilegeDOS = levelPrivilegeMapper.selectBatchIds(command.getIds());
        levelPrivilegeMapper.deleteBatchIds(command.getIds());
        List<DeleteLevelPrivilegeOperateDTO> deleteLevelPrivilegeOperateDTOS = levelConverter.levelPrivilegeDOsToDeleteLevelPrivilegeOperateDTOs(levelPrivilegeDOS);
        return deleteLevelPrivilegeOperateDTOS;
    }
}
